<template>
	<view>
		<van-cell-group title="发帖子">
			<van-field custom-style="height:500rpx" auto-focus :value="text" @change="getInput" type="textarea"
				placeholder="说点什么..." autosize auto-focus />
		</van-cell-group>
		<van-cell-group title="上传照片" :border="false">
			<van-row>
				<van-uploader :disabled="disable" :file-list="previewList" @after-read="previewImg" max-count="9" />
			</van-row>
		</van-cell-group>
		<van-row>
			<view class="submitBtn">
				<van-button :disabled="disable" type="primary" color="#006600" @click="submit" block>
					发布
				</van-button>
			</view>
		</van-row>
		<van-notify id="van-notify" />
		<van-dialog id="van-dialog" confirm-button-color="#006600" @tap="getUserInfoByOpenid(e)" />
	</view>
</template>
<script>
	import Notify from '../../wxcomponents/vant/dist/notify/notify';
	import Dialog from '../../wxcomponents/vant/dist/dialog/dialog';
	export default {
		data() {
			return {
				text: "",
				school: "",
				disable: true,
				postId: "",
				fileList: [],
				previewList: [],
				imgId: 0,
				file: null
			}
		},
		onLoad() {
			this.getUserInfo()
		},
		onShow() {
			this.getUserInfo()
		},
		methods: {
			addAUserInfo(res) {
				uni.request({
					url: getApp().globalData.baseURL + "/userInf/addAUserInf",
					data: {
						"openid": wx.getStorageSync('openid'),
						"nickname": res.userInfo.nickName,
						"headImg": res.userInfo.avatarUrl
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {
						console.log(res)
					},
					fail: (err) => {
						Notify({
							type: 'danger',
							message: '添加用户信息失败'
						});
					}
				})
			},
			getUserInfoByOpenid(e) {
				let self = this
				// 查询用户信息
				uni.request({
					url: getApp().globalData.baseURL + "/userInf/getUserInfByOpenid",
					data: {
						"openid": wx.getStorageSync('openid')
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {
						if (res.data.data === null) {
							wx.getUserProfile({
								desc: "用于完善用户资料",
								success: (res) => {
									self.addAUserInfo(res)
								},
								fail: (err) => {
									Notify({
										type: 'danger',
										message: '查询用户信息失败'
									});
								}
							})
						}
					},
					fail: (err) => {
						console.log(err)
					}
				})
			},
			getInput(e) {
				this.text = e.detail
				this.checkNull()
			},
			previewImg(e) {
				const {
					previewFile
				} = e.detail
				this.previewList.push({
					...previewFile,
					url: e.detail.file.url
				})
			},
			submit() {
				wx.showLoading({
					title: "开始上传..."
				})
				let self = this
				uni.request({
					url: getApp().globalData.baseURL + "/post/addAPost",
					data: {
						"openid": wx.getStorageSync('openid'),
						"school": self.school,
						"content": self.text
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {
						let self = this
						self.postId = res.data.data
						let length = self.previewList.length
						if (length !== 0) {
							for (let i = 0; i < length; i++) {
								wx.uploadFile({
									url: getApp().globalData.baseURL + "/post/addAPostImage",
									filePath: self.previewList[i].url,
									name: "file",
									formData: {
										"file": self.previewList[i],
										"imageId": i,
										"postId": self.postId
									},
									header: {
										"token": wx.getStorageSync('token')
									},
									success: (res) => {
										self.fileList.push({
											...self.previewList[i],
											url: res.data
										});
										if (self.previewList.length === self.fileList.length) {
											wx.hideLoading()
											uni.navigateBack()
										}
									},
									fail: (err) => {
										console.log(err)
										wx.hideLoading()
										Notify({
											type: 'danger',
											message: '发布失败,请检查您的网络'
										});
									}
								})
							}
						} else {
							wx.hideLoading()
						}
						uni.navigateBack()
					},
					fail: (err) => {
						Notify({
							type: 'danger',
							message: '失败,请检查您的网络'
						});
					}
				})
			},
			checkNull() {
				if (this.school !== "" && this.text !== "") {
					this.disable = false
				} else {
					if (this.school === "") {
						Notify({
							type: 'danger',
							message: '请登录'
						});
					}
					this.disable = true
				}
			},
			getUserInfo() {
				let self = this
				uni.request({
					url: getApp().globalData.baseURL + "/user/getUser",
					data: {
						"openid": wx.getStorageSync('openid')
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {
						console.log(res)
						if (res.data.code === 1) {
							if (res.data.data.headImg !== null) {
								self.school = res.data.data.school
							} else {
								// 未完善个人信息,需要拿到学校,昵称
								Dialog.alert({
									message: '您还未完善个人信息,只有完善信息后才能发帖哦',
									confirmButtonText: '去完善'
								}).then(() => {
									uni.navigateTo({
										url: "../myInfo/myInfo"
									})
								});
							}

						} else {
							Notify({
								type: 'danger',
								message: '请重试'
							});
						}

					},
					fail: (err) => {
						Notify({
							type: 'danger',
							message: '获取个人信息失败,请检查您的网络连接'
						});
					}
				})
			}
		}
	}
</script>

<style scoped>
	.submitBtn {
		margin: 30% auto;
		border-radius: 10rpx;
		margin-left: 20rpx;
		margin-right: 20rpx;
		text-align: center;
	}
</style>
